package top.yunmouren.webui.cmd;

import com.mojang.brigadier.CommandDispatcher;
import javafx.application.Platform;
import javafx.stage.Stage;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.minecraft.server.command.ServerCommandSource;
import top.yunmouren.webui.Engine.Engine;
import top.yunmouren.webui.client.Process;

import static com.mojang.brigadier.arguments.StringArgumentType.getString;
import static com.mojang.brigadier.arguments.StringArgumentType.string;
import static net.minecraft.server.command.CommandManager.argument;
import static net.minecraft.server.command.CommandManager.literal;


public class webkit {
    public String Cmd = "webkit";
    public int PermissionLevel = 4;
    public static top.yunmouren.webui.Engine.Engine Engine = new Engine();
    public boolean isRun = true;

    public webkit() {
        CommandRegistrationCallback.EVENT.register(this::register);
    }

    public void register(CommandDispatcher<ServerCommandSource> dispatcher, boolean dedicated) {
        dispatcher.register(literal(Cmd)
                .requires(source -> source.hasPermissionLevel(PermissionLevel))
                .then(
                        argument("url", string())
                                .executes(c -> {
                                    if (isRun) {
                                        Engine.main();
                                        isRun = false;
                                    } else {
                                        Platform.runLater(() -> {
                                            Engine.start(new Stage());
                                        });
                                    }
                                    Engine.Url = "http://" + getString(c, "url");
                                    return 1;
                                })
                )
                .executes(context -> {
                    return 1;
                }));
    }
}
